<template>
  <!-- 添加或修改入库单对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="90%" append-to-body :close-on-click-modal="false" v-dialogDrag>
      <el-descriptions title="产品信息" border>
        <el-descriptions-item label="产品编码">{{ form.params.goodsCode }}</el-descriptions-item>
        <el-descriptions-item label="产品名称">{{ form.params.goodsName }}</el-descriptions-item>
        <el-descriptions-item label="产品型号">{{ form.params.goodsModel }}</el-descriptions-item>
        <el-descriptions-item label="产品数量">{{ form.daohuoCount }}</el-descriptions-item>
        <el-descriptions-item label="重量">{{ form.dun }}</el-descriptions-item>
        <el-descriptions-item label="单重">{{ form.goodsBuyPrice }}</el-descriptions-item>
        <el-descriptions-item label="总价">{{ form.goodsBuyTotal }}</el-descriptions-item>
        <el-descriptions-item label="单位">{{ form.unit }}</el-descriptions-item>
      </el-descriptions>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">

         <el-col :span="12">
          <el-form-item label="出库人" prop="rukupeople">
            <el-input v-model="form.rukupeople" placeholder="请输入出库人" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="出库日期" prop="rukuDate">
            <el-date-picker clearable v-model="form.rukuDate" type="datetime" value-format="yyyy-MM-dd"
              placeholder="请选择出库日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" placeholder="请输入备注" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="订单编号" prop="projectNo" size="mini">
            <el-select v-model="projectNos" filterable remote reserve-keyword placeholder="请输入订单编号"
              :remote-method="search" :loading="loading3" @change="changeBySort">
              <el-option v-for="item in orderList" :key="item.code" :label="item.code" :value="item.code">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
    </el-form>
      <el-row :gutter="20">
        <el-table border v-loading="loading" :data="dataList" @selection-change="handleSelectionChange" ref="table"
          :max-height="tableMaxHeight" show-summary :summary-method="getSummaries">
          <el-table-column align="center" type="selection" width="55" />
          <el-table-column align="center" label="序号" type="index" width="50">
            <template slot-scope="scope">
              <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
            </template>
          </el-table-column>
          <el-table-column label="框架编号" align="center" prop="order.projectCode" show-overflow-tooltip />
          <el-table-column label="订单编号" align="center" prop="projectNo" sortable />
          <el-table-column label="创建日期" align="center" prop="createTime" sortable show-overflow-tooltip />
          <el-table-column label="工程名称" align="center" prop="gongchengName" show-overflow-tooltip sortable />
          <el-table-column sortable align="center" label="产品编码" prop="goodsCode">
            <template slot-scope="scope">
              <el-tooltip class="item" content="点击查看详情" effect="dark" placement="left-start">
                <span @click="showPuduceSaleAll(scope.row)" style="color: #00afff;cursor:pointer;">
                  {{ scope.row.goodsCode }}</span>
              </el-tooltip>
            </template>
          </el-table-column>
          <el-table-column label="产品状态" align="center" prop="status" sortable>
            <template slot-scope="scope">
              <dict-tag :options="dict.type.goods_status" :value="scope.row.status" />
            </template>
          </el-table-column>
          <el-table-column align="center" label="产品名称" prop="goodsName" sortable />
          <el-table-column align="center" label="合同物料名称" prop="contractGoodsName" sortable />
          <el-table-column align="center" label="合同收货人" prop="deliveryPerson" sortable />
          <el-table-column align="center" label="产品型号" prop="model" sortable />
          <el-table-column align="center" label="产品单位" prop="unit" sortable />
          <el-table-column align="center" label="销售单价" prop="goodsUnitPrice" sortable />
          <el-table-column align="center" label="销售总价" prop="goodsTotalPrice" sortable />
          <el-table-column align="center" label="制单人" prop="order.createName" />
          <el-table-column align="center" label="订单数量" prop="orderCount" sortable />
          <el-table-column label="是否自制" align="center" prop="isdo" sortable>
            <template slot-scope="scope">
              <dict-tag :options="dict.type.isdo" :value="scope.row.isdo" />
            </template>
          </el-table-column>
          <el-table-column align="center" label="备注" prop="remark" sortable />
          <el-table-column align="center" label="剩余数量" prop="canCount" />
          <el-table-column align="center" label="领取数量" width="210">
            <template slot-scope="scope">
              <el-input-number @change="() => handleChange(scope.row)" size="mini" v-model="scope.row.useCount" :min="0"
                :max="maxValue(scope.row.canCount)"></el-input-number>
            </template>
          </el-table-column>
          <el-table-column align="center" label="领取总价" prop="totalPrice" />
        </el-table>
      </el-row>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定 出 库</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addAction, deleteAction, getAction, getById, updateAction, isEmpty, accMul,accAdd } from '@/api/manage';
import moment from 'moment';

export default {
  name: "CkumaterialsForm",
  dicts: ['churuku_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      activeName: "first",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},//入库产品信息
      type: null,
      loadingSupplier: false,
      optionsSupplier: [],
      dataList: [],
      orderList: [],
      projectNos: [],
      loading3: false,
      goodsList: [],
      queryParams: {
        orderByColumn: "a.sort",
        isAsc: "asc",
        parentId: null,
        pageSizeZero: true,
      },
      goodsQueryParams: {
        name: null,
        pageNum: 1,
        pageSize: 20,
      },
      // 表单校验
      rules: {
        type: [{ required: true, message: "请选择出入库类别", trigger: "change" }],
        rukupeople: [{ required: true, message: "入库人不能为空", trigger: "change" }],
        rukuDate: [{ required: true, message: "请选择出入库日期", trigger: "change" }],
      },
      tableMaxHeight: window.innerHeight - 320,
      url: {
        goodsSaleList: '/goodsSale/goodsSale/list'
      },
    };
  },
  created() {
  },
  methods: {
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        const values = data.map((item) => Number(item[column.property]));
        // 只合计amount1和amount3，想合并哪些列就在这里指定
        if (column.property === "daohuoCount" || column.property === "dun" || column.property === "goodsBuyPrice" || column.property === "goodsBuyTotal") {
          sums[index] = values.reduce((prev, curr) => {

            const value = Number(curr);
            if (!isNaN(value)) {
              return accAdd(prev, curr);
            } else {
              return prev;
            }
          }, 0);
        }
      });
      return sums;
    },
    // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
    // 选择属性
    changeBySort(e) {
      var params = {
        "isAsc": 'asc',
        "orderByColumn": "sort",
        "projectNo": e
      };
      console.log(e)
      getAction("/goodsSale/goodsSale/list", params).then(response => {
        this.dataList = response.rows;
        this.loading3 = false;
      });
    },
    updateTotalCost(row) {
      let that = this;
      if (row.goodsType === "0") {
        if (row.daohuoCount && row.goodsBuyPrice) {
          let totleCost = accMul(row.daohuoCount, row.goodsBuyPrice);
          that.$set(row, "goodsBuyTotal", totleCost);
        }
      }
      if (row.goodsType === "1") {
        if (row.dun && row.goodsBuyPrice) {
          let totleCost = accMul(row.dun, row.goodsBuyPrice);
          that.$set(row, "goodsBuyTotal", totleCost);
        }
      }
    },
    async search(queryString) {
      this.loading3 = true;
      if (queryString) {
        try {
          let params = {
            // "pageSizeZero": true,
            "pageNum": 1,
            "pageSize": 20,
            "code": queryString

          };
          getAction("/projectBaseInfo/projectBaseInfo/list", params).then(response => {
            this.orderList = response.rows;
            this.loading3 = false;
          });

        } catch (error) {
          console.error('Error fetching data:', error);
          this.loading3 = false;
        }
      } else {
        this.orderList = [];
        this.loading3 = false;
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        code: null,
        name: null,
        mainId: null,
        nos: null,
        ddDate: null,
        subDate: null,
        type: null,
        ywType: null,
        customerId: null,
        customerName: null,
        isBuy: null,
        money: null,
        counts: null,
        dunwei: null,
        supplierId: null,
        supplierName: null,
        deptId: null,
        deptName: null,
        userId: null,
        userName: null,
        state: null,
        rukupeople: this.$store.getters.nickName,
        rukuid: this.$store.getters.userId,
        rukuDate: new Date(),
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd(row, type) {
      this.reset();
      this.form = row
      this.form.type = type;
      this.open = true;
      this.title = type == 1 ? "添加成品出库单" : "添加材料出库单";
      this.dataList = [];
      this.loading = false;
    },
    /** 提交按钮 */
    submitForm() {

    },
  }
};
</script>
